package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.User;
import service.UserService;

import java.util.List;

@Controller
@RequestMapping("/user")

// /user/list
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user) {
       boolean flag = userService.login(user);
       if(flag) {
           return "index";
       } else {
           return "login";
       }
    }
    @RequestMapping("/list")
    public String findAll() {
        List<User> users = userService.findAll(null, 0, 10);
        return "user/list";
    }

    @RequestMapping("/toadd")
    public String toAdd() {
        return "user/add";
    }
    @RequestMapping("/addSave")
    public String addUser(User user) {
        int result = userService.addUser(user);
        if(result>0){
            return "redirect:/user/list";
        }else {
            return "error";
        }
    }

    @RequestMapping("/delete")
    public String deleteUser(@pathVariable("id") int id) {
        userService.deleteUser(id);
        return "index";
    }

    @RequestMapping("/updateSave")
    public String updateUser() {
        return "index";
    }

    @RequestMapping("/toupdate")
    public String toUpdateUser(@pathVariable("id") int id) {
        return "index";
    }


}
